package com.qfmy.webapp.service.impl.wallpaper;

import com.qfmy.model.entity.wallpaper.Avatar;
import com.qfmy.model.vo.wallpaper.AvatarVo;
import com.qfmy.webapp.mapper.wallpaper.AvatarMapper;
import com.qfmy.webapp.service.wallpaper.AvatarService;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author 清风明月
 * @Package com.qfmy.web.app.service.impl.wallpaper
 * @date 2025/11/26
 * @description 头像
 */
@Service
@SuppressWarnings("all")
public class AvatarServiceImpl implements AvatarService {

    /**
     * 注入mapper
     */
    @Autowired
    private AvatarMapper avatarMapper;

    /**
     * 注入redis
     */
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 获取精选头像
     *
     * @return
     */
    @Override
    public List<AvatarVo> getJingXuanAvatars() {
        //创建缓存key
        String key = "jingXuanAvatars:";
        //从缓存里面获取数据
        Object list =  redisTemplate.opsForValue().get(key);
        if (list != null) {
            return (List<AvatarVo>) list;
        }
        //缓存没有命中
       List<Avatar> avatars = avatarMapper.getJingXuanAvatars();
        //转换为VO
        List<AvatarVo> avatarVos = convertToVo(avatars);
        //缓存数据
        if(avatarVos.size() > 0)
        {
            redisTemplate.opsForValue().set(key, avatarVos, 24, TimeUnit.HOURS);
        }else{
            redisTemplate.opsForValue().set(key, avatarVos, 1, TimeUnit.SECONDS);
        }
        return avatarVos;
    }

    /**
     * 根据分类ID获取头像
     * @param categoryId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<AvatarVo> getAvatarsByCategoryId(Integer categoryId, Integer pageNum, Integer pageSize) {
        //校验分页对象
        if (pageNum < 1 || pageSize < 1) {
            pageNum = 1;
            pageSize = 15;
        }
        //计算分页
        int offset = (pageNum - 1) * pageSize;
        //创建缓存key
        String key = "avatars:categoryId:" + categoryId + ":" + pageNum + ":" + pageSize;
        //从缓存里面获取数据
        Object list =  redisTemplate.opsForValue().get(key);
        if (list != null) {
            return (List<AvatarVo>) list;
        }
        //缓存没有命中
        List<Avatar> avatars = avatarMapper.getAvatarsByCategoryId(categoryId, offset, pageSize);
        //转换为VO
        List<AvatarVo> avatarVos = convertToVo(avatars);
        //缓存数据
        Long ttl = avatarVos.isEmpty() ? 1 : 60 * 60 * 24L;
        redisTemplate.opsForValue().set(key, avatarVos, ttl, TimeUnit.SECONDS);
        //返回结果
        return avatarVos;
    }

    /**
     * 更新下载次数
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDownloadCount(Integer id) {
        //更新下载次数
        avatarMapper.updateDownloadCount(id);
    }

    /**
     * 转换为VO
     * @param avatars
     * @return
     */
    @Nullable
    @Contract(pure = true)
    private List<AvatarVo> convertToVo(List<Avatar> avatars) {
        //判断集合是否为空
        if (avatars != null && !avatars.isEmpty()) {
            return avatars.stream().map(avatar -> {
                AvatarVo avatarVo = new AvatarVo();
                avatarVo.setId(avatar.getId());
                avatarVo.setImageUrl(avatar.getImageUrl());
                return avatarVo;
            }).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}
